<template><div><h2 id="消息通知" tabindex="-1"><a class="header-anchor" href="#消息通知"><span>消息通知</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#generating-notifications">生成通知</a></li>
<li><a href="#sending-notifications">发送通知</a>
<ul>
<li><a href="#using-the-notifiable-trait">使用可通知特征</a></li>
<li><a href="#using-the-notification-facade">使用通知门面</a></li>
<li><a href="#specifying-delivery-channels">指定传送渠道</a></li>
<li><a href="#queueing-notifications">通知排队</a></li>
<li><a href="#on-demand-notifications">按需通知</a></li>
</ul>
</li>
<li><a href="#mail-notifications">邮件通知</a>
<ul>
<li><a href="#formatting-mail-messages">格式化邮件消息</a></li>
<li><a href="#customizing-the-sender">自定义发件人</a></li>
<li><a href="#customizing-the-recipient">自定义收件人</a></li>
<li><a href="#customizing-the-subject">自定义主题</a></li>
<li><a href="#customizing-the-mailer">自定义邮件程序</a></li>
<li><a href="#customizing-the-templates">自定义模板</a></li>
<li><a href="#mail-attachments">附件</a></li>
<li><a href="#adding-tags-metadata">添加标签和元数据</a></li>
<li><a href="#customizing-the-symfony-message">自定义 Symfony 消息</a></li>
<li><a href="#using-mailables">使用 Mailables</a></li>
<li><a href="#previewing-mail-notifications">预览邮件通知</a></li>
</ul>
</li>
<li><a href="#markdown-mail-notifications">Markdown 邮件通知</a>
<ul>
<li><a href="#generating-the-message">生成消息</a></li>
<li><a href="#writing-the-message">撰写消息</a></li>
<li><a href="#customizing-the-components">自定义组件</a></li>
</ul>
</li>
<li><a href="#database-notifications">数据库通知</a>
<ul>
<li><a href="#database-prerequisites">先决条件</a></li>
<li><a href="#formatting-database-notifications">格式化数据库通知</a></li>
<li><a href="#accessing-the-notifications">访问通知</a></li>
<li><a href="#marking-notifications-as-read">将通知标记为已读</a></li>
</ul>
</li>
<li><a href="#broadcast-notifications">广播通知</a>
<ul>
<li><a href="#broadcast-prerequisites">先决条件</a></li>
<li><a href="#formatting-broadcast-notifications">格式化广播通知</a></li>
<li><a href="#listening-for-notifications">监听通知</a></li>
</ul>
</li>
<li><a href="#sms-notifications">短信通知</a>
<ul>
<li><a href="#sms-prerequisites">先决条件</a></li>
<li><a href="#formatting-sms-notifications">格式化短信通知</a></li>
<li><a href="#formatting-shortcode-notifications">格式化短代码通知</a></li>
<li><a href="#customizing-the-from-number">自定义「来源」号码</a></li>
<li><a href="#adding-a-client-reference">添加客户参考</a></li>
<li><a href="#routing-sms-notifications">路由短信通知</a></li>
</ul>
</li>
<li><a href="#slack-notifications">Slack 通知</a>
<ul>
<li><a href="#slack-prerequisites">先决条件</a></li>
<li><a href="#formatting-slack-notifications">格式化 Slack 通知</a></li>
<li><a href="#slack-attachments">Slack 附件</a></li>
<li><a href="#routing-slack-notifications">路由 Slack 通知</a></li>
</ul>
</li>
<li><a href="#localizing-notifications">本地化通知</a></li>
<li><a href="#testing">测试</a></li>
<li><a href="#notification-events">通知事件</a></li>
<li><a href="#custom-channels">自定义渠道</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p>除了支持 <a href="https://learnku.com/docs/laravel/10.x/mail" target="_blank" rel="noopener noreferrer">发送电子邮件</a> 之外，Laravel还提供了支持通过多种传递渠道发送通知的功能，包括电子邮件、短信（通过 <a href="https://www.vonage.com/communications-apis/" target="_blank" rel="noopener noreferrer">Vonage</a>，前身为Nexmo）和 <a href="https://slack.com/" target="_blank" rel="noopener noreferrer">Slack</a>。此外，已经创建了多种 <a href="https://laravel-notification-channels.com/about/#suggesting-a-new-channel" target="_blank" rel="noopener noreferrer">社区构建的通知渠道</a>，用于通过数十个不同的渠道发送通知！通知也可以存储在数据库中，以便在你的Web界面中显示。</p>
<p>通常，通知应该是简短的信息性消息，用于通知用户应用中发生的事情。例如，如果你正在编写一个账单应用，则可以通过邮件和短信频道向用户发送一个「支付凭证」通知。</p>
<h2 id="创建通知" tabindex="-1"><a class="header-anchor" href="#创建通知"><span>创建通知</span></a></h2>
<p>Laravel 中，通常每个通知都由一个存储在 <code v-pre>app/Notifications</code> 目录下的一个类表示。如果在你的应用中没有看到这个目录，不要担心，当运行 <code v-pre>make:notification</code> 命令时它将为你创建：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:notification InvoicePaid</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>这个命令会在 <code v-pre>app/Notifications</code> 目录下生成一个新的通知类。每个通知类都包含一个 <code v-pre>via</code> 方法以及一个或多个消息构建的方法比如 <code v-pre>toMail</code> 或 <code v-pre>toDatabase</code>，它们会针对特定的渠道把通知转换为对应的消息。</p>
<h2 id="发送通知" tabindex="-1"><a class="header-anchor" href="#发送通知"><span>发送通知</span></a></h2>
<h3 id="使用-notifiable-trait" tabindex="-1"><a class="header-anchor" href="#使用-notifiable-trait"><span>使用 Notifiable Trait</span></a></h3>
<p>通知可以通过两种方式发送： 使用 <code v-pre>Notifiable</code> 特性的 <code v-pre>notify</code> 方法或使用 <code v-pre>Notification</code> <a href="https://learnku.com/docs/laravel/10.x/facades" target="_blank" rel="noopener noreferrer">facade</a>。 该 <code v-pre>Notifiable</code> 特性默认包含在应用程序的 <code v-pre>App\Models\User</code> 模型中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此 <code v-pre>notify</code> 方法需要接收一个通知实例参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>InvoicePaid</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>技巧</strong><br>
请记住，你可以在任何模型中使用 <code v-pre>Notifiable</code> trait。而不仅仅是在 <code v-pre>User</code> 模型中。</p>
</blockquote>
<h3 id="使用-notification-facade" tabindex="-1"><a class="header-anchor" href="#使用-notification-facade"><span>使用 Notification Facade</span></a></h3>
<p>另外，你可以通过 <code v-pre>Notification</code> <a href="https://learnku.com/docs/laravel/10.x/facades" target="_blank" rel="noopener noreferrer">facade</a> 来发送通知。它主要用在当你需要给多个可接收通知的实体发送的时候，比如给用户集合发送通知。使用 Facade 发送通知的话，要把可接收通知实例和通知实例传递给 <code v-pre>send</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token variable">$users</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以使用 <code v-pre>sendNow</code> 方法立即发送通知。即使通知实现了 <code v-pre>ShouldQueue</code> 接口，该方法也会立即发送通知：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">sendNow</span><span class="token punctuation">(</span><span class="token variable">$developers</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">DeploymentCompleted</span><span class="token punctuation">(</span><span class="token variable">$deployment</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="发送指定频道" tabindex="-1"><a class="header-anchor" href="#发送指定频道"><span>发送指定频道</span></a></h3>
<p>每个通知类都有一个 <code v-pre>via</code> 方法，用于确定将在哪些通道上传递通知。通知可以在 <code v-pre>mail</code>、<code v-pre>database</code>、<code v-pre>broadcast</code>、<code v-pre>vonage</code> 和 <code v-pre>slack</code> 频道上发送。</p>
<blockquote>
<p><strong>提示</strong><br>
如果你想使用其他的频道，比如 Telegram 或者 Pusher，你可以去看下社区驱动的 <a href="http://laravel-notification-channels.com/" target="_blank" rel="noopener noreferrer">Laravel 通知频道网站</a>.</p>
</blockquote>
<p><code v-pre>via</code> 方法接收一个 <code v-pre>$notifiable</code> 实例，这个实例将是通知实际发送到的类的实例。你可以用 <code v-pre>$notifiable</code> 来决定这个通知用哪些频道来发送：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知发送频道。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;int, string></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">via</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$notifiable</span><span class="token operator">-></span><span class="token property">prefers_sms</span> <span class="token operator">?</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'vonage'</span><span class="token punctuation">]</span> <span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'mail'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'database'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="通知队列化" tabindex="-1"><a class="header-anchor" href="#通知队列化"><span>通知队列化</span></a></h3>
<blockquote>
<p><strong>注意</strong><br>
使用通知队列前需要配置队列并 <a href="https://learnku.com/docs/laravel/10.x/queues" target="_blank" rel="noopener noreferrer">开启一个队列任务</a>。</p>
</blockquote>
<p>发送通知可能是耗时的，尤其是通道需要调用额外的 API 来传输通知。为了加速应用的响应时间，可以将通知推送到队列中异步发送，而要实现推送通知到队列，可以让对应通知类实现 <code v-pre>ShouldQueue</code> 接口并使用 <code v-pre>Queueable</code> trait。如果通知类是通过 make:notification 命令生成的，那么该接口和 trait 已经默认导入，你可以快速将它们添加到通知类：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Notifications</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>ShouldQueue</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">InvoicePaid</span> <span class="token keyword">extends</span> <span class="token class-name">Notification</span> <span class="token keyword">implements</span> <span class="token class-name">ShouldQueue</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦将 <code v-pre>ShouldQueue</code> 接口添加到你的通知中，你就可以发送通知。 Laravel 将检测类上的 <code v-pre>ShouldQueue</code> 接口并自动排队发送通知：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>排队通知时，将为每个收件人和频道组合创建一个排队的作业。比如，如果你的通知有三个收件人和两个频道，则六个作业将被分配到队列中。</p>
<h4 id="延迟通知" tabindex="-1"><a class="header-anchor" href="#延迟通知"><span>延迟通知</span></a></h4>
<p>如果你需要延迟发送消息通知, 你可以在你的消息通知实例上添加 <code v-pre>delay</code> 方法:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$delay</span> <span class="token operator">=</span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinutes</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">delay</span><span class="token punctuation">(</span><span class="token variable">$delay</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="多个通道的延迟通知" tabindex="-1"><a class="header-anchor" href="#多个通道的延迟通知"><span>多个通道的延迟通知</span></a></h4>
<p>将一个数组传递给 <code v-pre>delay</code> 方法来指定特定通道的延迟时间:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">delay</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'mail'</span> <span class="token operator">=></span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinutes</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'sms'</span> <span class="token operator">=></span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinutes</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，你可以在通知类本身上定义一个 <code v-pre>withDelay</code> 方法。 <code v-pre>withDelay</code> 方法会返回包含通道名称和延迟值的数组:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 确定通知的传递延迟.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, \Illuminate\Support\Carbon></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">withDelay</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'mail'</span> <span class="token operator">=></span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinutes</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'sms'</span> <span class="token operator">=></span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinutes</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义消息通知队列连接" tabindex="-1"><a class="header-anchor" href="#自定义消息通知队列连接"><span>自定义消息通知队列连接</span></a></h4>
<p>默认情况下，排队的消息通知将使用应用程序的默认队列连接进行排队。如果你想指定一个不同的连接用于特定的通知，你可以在通知类上定义一个 <code v-pre>$connection</code> 属性:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 排队通知时要使用的队列连接的名称.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token variable">$connection</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'redis'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，如果你想为每个通知通道都指定一个特定的队列连接，你可以在你的通知上定义一个 <code v-pre>viaConnections</code> 方法。这个方法应该返回一个通道名称 / 队列连接名称的数组。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 定义每个通知通道应该使用哪个连接。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, string></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">viaConnections</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'mail'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'redis'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'database'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'sync'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义通知通道队列" tabindex="-1"><a class="header-anchor" href="#自定义通知通道队列"><span>自定义通知通道队列</span></a></h4>
<p>如果你想为每个通知通道指定一个特定的队列，你可以在你的通知上定义一个 <code v-pre>viaQueues</code> 。 此方法应返回通道名称 / 队列名称对的数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 定义每个通知通道应使用哪条队列。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, string></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">viaQueues</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'mail'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'mail-queue'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'slack'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'slack-queue'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="队列通知-数据库事务" tabindex="-1"><a class="header-anchor" href="#队列通知-数据库事务"><span>队列通知 &amp; 数据库事务</span></a></h4>
<p>当队列通知在数据库事务中被分发时，它们可能在数据库事务提交之前被队列处理。发生这种情况时，你在数据库事务期间对模型或数据库记录所做的任何更新可能尚未反映在数据库中。甚至，在事务中创建的任何模型或数据库记录可能不存在于数据库中。如果你的通知依赖于这些模型，那么在处理发送队列通知时可能会发生意外错误。</p>
<p>如果你的队列连接的 <code v-pre>after_commit</code> 配置选项设置为 <code v-pre>false</code>，你仍然可以通过在发送通知时调用 <code v-pre>afterCommit</code> 方法来指示应在提交所有打开的数据库事务后发送特定的排队通知：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>InvoicePaid</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">afterCommit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，你可以从通知的构造函数调用 <code v-pre>afterCommit</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Notifications</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>ShouldQueue</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">InvoicePaid</span> <span class="token keyword">extends</span> <span class="token class-name">Notification</span> <span class="token keyword">implements</span> <span class="token class-name">ShouldQueue</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 创建一个新的通知通知实例。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">__construct</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">afterCommit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
要了解更多解决这些问题的方法，请查阅有关队列作业和 <a href="https://learnku.com/docs/laravel/10.x/queuesmd#jobs-and-database-transactions" target="_blank" rel="noopener noreferrer">数据库事务</a> 的文档。</p>
</blockquote>
<h4 id="确定是否发送排队的通知" tabindex="-1"><a class="header-anchor" href="#确定是否发送排队的通知"><span>确定是否发送排队的通知</span></a></h4>
<p>在将排队的通知分派到后台处理的队列之后，它通常会被队列工作进程接受并发送给其目标收件人。</p>
<p>然而，如果你想要在队列工作进程处理后最终确定是否发送排队的通知，你可以在通知类上定义一个 <code v-pre>shouldSend</code> 方法。如果此方法返回 <code v-pre>false</code>，则通知不会被发送：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 定义通知是否应该被发送。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">shouldSend</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$channel</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">bool</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token function">isPaid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="按需通知" tabindex="-1"><a class="header-anchor" href="#按需通知"><span>按需通知</span></a></h3>
<p>有时你需要向一些不属于你应用程序的「用户」发送通知。使用 <code v-pre>Notification</code> 门面的 <code v-pre>route</code> 方法，你可以在发送通知之前指定即时的通知路由信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Broadcasting<span class="token punctuation">\</span>Channel</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'vonage'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'5555555555'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'slack'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'https://hooks.slack.com/services/...'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'broadcast'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token keyword">new</span> <span class="token class-name">Channel</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'channel-name'</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想在向 <code v-pre>mail</code> 路由发送通知时指定收件人，你可以提供一个数组<br>
电子邮件地址作为键，名字作为值。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'barrett@example.com'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Barrett Blair'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="邮件通知" tabindex="-1"><a class="header-anchor" href="#邮件通知"><span>邮件通知</span></a></h2>
<h3 id="格式化邮件" tabindex="-1"><a class="header-anchor" href="#格式化邮件"><span>格式化邮件</span></a></h3>
<p>如果一个通知支持以电子邮件的形式发送，你应该在通知类中定义一个 <code v-pre>toMail</code> 方法。这个方法将接收一个 <code v-pre>$notifiable</code> 实体，并应该返回一个<code v-pre>Illuminate\Notifications\Messages\MailMessage</code> 实例。</p>
<p><code v-pre>MailMessage</code> 类包含一些简单的方法来帮助你建立事务性的电子邮件信息。邮件信息可能包含几行文字以及一个「操作」。让我们来看看一个 <code v-pre>toMail</code> 方法的例子。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件表示形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/invoice/'</span><span class="token operator">.</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">greeting</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'你好!'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'你的一张发票已经付款了！'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">lineIf</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">amount</span> <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"支出金额: <span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">amount</span><span class="token punctuation">}</span></span>"</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">action</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'查看发票'</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'感谢你使用我们的应用程序！'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
注意我们在 <code v-pre>toMail</code> 方法中使用 <code v-pre>$this-&gt;invoice-&gt;id</code>。你可以在通知的构造函数中传递任何你的通知需要生成的信息数据。</p>
</blockquote>
<p>在这个例子中，我们注册了一个问候语、一行文字、一个操作，然后是另一行文字。<code v-pre>MailMessage</code> 对象所提供的这些方法使得邮件的格式化变得简单而快速。然后，邮件通道将把信息组件转换封装成一个漂亮的、响应式的HTML电子邮件模板，并有一个纯文本对应。<br>
下面是一个由 <code v-pre>mail</code> 通道生成的电子邮件的例子。</p>
<p><img src="https://laravel.com/img/docs/notification-example-2.png" alt="notification-example-2.png"></p>
<blockquote>
<p><strong>注意</strong><br>
当发送邮件通知时，请确保在你的 <code v-pre>config/app.php</code> 配置文件中设置 <code v-pre>name</code> 配置选项。<br>
这个值将在你的邮件通知信息的标题和页脚中使用。</p>
</blockquote>
<h4 id="错误消息" tabindex="-1"><a class="header-anchor" href="#错误消息"><span>错误消息</span></a></h4>
<p>一些通知会通知用户错误，比如支付失败的发票。你可以通过在构建消息时调用 <code v-pre>error</code> 方法来指示邮件消息是关于错误的。当在邮件消息上使用 <code v-pre>error</code> 方法时，操作按钮将会是红色而不是黑色：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件表示形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">error</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">subject</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'发票支付失败'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'...'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="其他邮件通知格式选项" tabindex="-1"><a class="header-anchor" href="#其他邮件通知格式选项"><span>其他邮件通知格式选项</span></a></h4>
<p>你可以使用 <code v-pre>view</code> 方法来指定应用于呈现通知电子邮件的自定义模板，而不是在通知类中定义文本「行」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件表现形式</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token string single-quoted-string">'emails.name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'invoice'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以通过将视图名称作为数组的第二个元素传递给 <code v-pre>view</code> 方法来指定邮件消息的纯文本视图：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件表现形式</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'emails.name.html'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'emails.name.plain'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'invoice'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义发件人" tabindex="-1"><a class="header-anchor" href="#自定义发件人"><span>自定义发件人</span></a></h3>
<p>默认情况下，电子邮件的发件人/寄件人地址在 <code v-pre>config/mail.php</code> 配置文件中定义。但是，你可以使用 <code v-pre>from</code> 方法为特定的通知指定发件人地址：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件表现形式</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'barrett@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Barrett Blair'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'...'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义收件人" tabindex="-1"><a class="header-anchor" href="#自定义收件人"><span>自定义收件人</span></a></h3>
<p>当通过 <code v-pre>mail</code> 通道发送通知时，通知系统将自动查找可通知实体的 <code v-pre>email</code> 属性。你可以通过在可通知实体上定义 <code v-pre>routeNotificationForMail</code> 方法来自定义用于传递通知的电子邮件地址：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 路由邮件通道的通知。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span>  <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, string>|string</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">routeNotificationForMail</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Notification</span> <span class="token variable">$notification</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span><span class="token operator">|</span><span class="token keyword type-declaration">string</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// 只返回电子邮件地址...</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">email_address</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 返回电子邮件地址和姓名...</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">email_address</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义主题" tabindex="-1"><a class="header-anchor" href="#自定义主题"><span>自定义主题</span></a></h3>
<p>默认情况下，邮件的主题是通知类的类名格式化为「标题案例」（Title Case）。因此，如果你的通知类命名为 <code v-pre>InvoicePaid</code>，则邮件的主题将是 <code v-pre>Invoice Paid</code>。如果你想为消息指定不同的主题，可以在构建消息时调用 <code v-pre>subject</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件表现形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">subject</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'通知标题'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'...'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义邮件程序" tabindex="-1"><a class="header-anchor" href="#自定义邮件程序"><span>自定义邮件程序</span></a></h3>
<p>默认情况下，邮件通知将使用 <code v-pre>config/mail.php</code> 配置文件中定义的默认邮件程序进行发送。但是，你可以在运行时通过在构建消息时调用 <code v-pre>mailer</code> 方法来指定不同的邮件程序：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件表现形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">mailer</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'postmark'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'...'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义模板" tabindex="-1"><a class="header-anchor" href="#自定义模板"><span>自定义模板</span></a></h3>
<p>你可以通过发布通知包的资源来修改邮件通知使用的 HTML 和纯文本模板。运行此命令后，邮件通知模板将位于 <code v-pre>resources/views/vendor/notifications</code> 目录中：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan vendor:publish <span class="token parameter variable">--tag</span><span class="token operator">=</span>laravel-notifications</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="附件" tabindex="-1"><a class="header-anchor" href="#附件"><span>附件</span></a></h3>
<p>要在电子邮件通知中添加附件，可以在构建消息时使用 <code v-pre>attach</code> 方法。<code v-pre>attach</code> 方法接受文件的绝对路径作为其第一个参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件表现形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">greeting</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'你好!'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">attach</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
通知邮件消息提供的 <code v-pre>attach</code> 方法还接受 <a href="https://learnku.com/docs/laravel/10.x/mailmd#attachable-objects" target="_blank" rel="noopener noreferrer">可附加对象</a>。请查阅全面的 <a href="https://learnku.com/docs/laravel/10.x/mailmd#attachable-objects" target="_blank" rel="noopener noreferrer">可附加对象</a> 文档以了解更多信息。</p>
</blockquote>
<p>当附加文件到消息时，你还可以通过将 <code v-pre>array</code> 作为 <code v-pre>attach</code> 方法的第二个参数来指定显示名称和/或 MIME 类型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件表现形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">greeting</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'你好!'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">attach</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'as'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token string single-quoted-string">'mime'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'application/pdf'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>与在可邮寄对象中附加文件不同，你不能使用 <code v-pre>attachFromStorage</code> 直接从存储磁盘附加文件。相反，你应该使用 <code v-pre>attach</code> 方法，并提供存储磁盘上文件的绝对路径。或者，你可以从 <code v-pre>toMail</code> 方法中返回一个 <a href="https://learnku.com/docs/laravel/10.x/mailmd#generating-mailables" target="_blank" rel="noopener noreferrer">可邮寄对象</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>InvoicePaid</span> <span class="token keyword">as</span> InvoicePaidMailable<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件表现形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Mailable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaidMailable</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">attachFromStorage</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>必要时，可以使用 <code v-pre>attachMany</code> 方法将多个文件附加到消息中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件表现形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">greeting</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'你好!'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">attachMany</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'/path/to/forge.svg'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token string single-quoted-string">'/path/to/vapor.svg'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                        <span class="token string single-quoted-string">'as'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Logo.svg'</span><span class="token punctuation">,</span></span>
<span class="line">                        <span class="token string single-quoted-string">'mime'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'image/svg+xml'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="原始数据附件" tabindex="-1"><a class="header-anchor" href="#原始数据附件"><span>原始数据附件</span></a></h4>
<p><code v-pre>attachData</code> 方法可以用于将原始字节数组附加为附件。在调用 <code v-pre>attachData</code> 方法时，应提供应分配给附件的文件名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件表现形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">greeting</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'你好!'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">attachData</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">pdf</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'mime'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'application/pdf'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="添加标签和元数据" tabindex="-1"><a class="header-anchor" href="#添加标签和元数据"><span>添加标签和元数据</span></a></h3>
<p>一些第三方电子邮件提供商（如 Mailgun 和 Postmark）支持消息「标签」和「元数据」，可用于分组和跟踪应用程序发送的电子邮件。可以通过 <code v-pre>tag</code> 和 <code v-pre>metadata</code> 方法将标签和元数据添加到电子邮件消息中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件表现形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">greeting</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'评论点赞！'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">tag</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'点赞'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">metadata</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comment_id'</span><span class="token punctuation">,</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">comment</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你的应用程序使用 Mailgun 驱动程序，则可以查阅 Mailgun 的文档以获取有关 <a href="https://documentation.mailgun.com/en/latest/user_manual.html#tagging-1" target="_blank" rel="noopener noreferrer">标签</a> 和 <a href="https://documentation.mailgun.com/en/latest/user_manual.html#attaching-data-to-messages" target="_blank" rel="noopener noreferrer">元数据</a> 的更多信息。同样，也可以参考 Postmark 文档了解他们对 <a href="https://postmarkapp.com/blog/tags-support-for-smtp" target="_blank" rel="noopener noreferrer">标签</a> 和 <a href="https://postmarkapp.com/support/article/1125-custom-metadata-faq" target="_blank" rel="noopener noreferrer">元数据</a> 的支持。</p>
<p>如果你的应用程序使用 Amazon SES 发送电子邮件，则应使用 <code v-pre>metadata</code> 方法将 <a href="https://docs.aws.amazon.com/ses/latest/APIReference/API_MessageTag.html" target="_blank" rel="noopener noreferrer">SES 「标签」</a>附加到消息。</p>
<h3 id="自定义-symfony-消息" tabindex="-1"><a class="header-anchor" href="#自定义-symfony-消息"><span>自定义 Symfony 消息</span></a></h3>
<p><code v-pre>MailMessage</code>类的<code v-pre>withSymfonyMessage</code>方法允许你注册一个闭包，在发送消息之前将调用Symfony Message实例。这给你在传递消息之前有深度自定义消息的机会：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Symfony<span class="token punctuation">\</span>Component<span class="token punctuation">\</span>Mime<span class="token punctuation">\</span>Email</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件表示形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">withSymfonyMessage</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Email</span> <span class="token variable">$message</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token variable">$message</span><span class="token operator">-></span><span class="token function">getHeaders</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addTextHeader</span><span class="token punctuation">(</span></span>
<span class="line">                        <span class="token string single-quoted-string">'Custom-Header'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Header Value'</span></span>
<span class="line">                    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="使用可邮寄对象" tabindex="-1"><a class="header-anchor" href="#使用可邮寄对象"><span>使用可邮寄对象</span></a></h3>
<p>如果需要，你可以从通知的 <code v-pre>toMail</code> 方法返回完整的 <a href="https://learnku.com/docs/laravel/10.x/mail" target="_blank" rel="noopener noreferrer">mailable 对象</a>。当返回 <code v-pre>Mailable</code> 而不是 <code v-pre>MailMessage</code> 时，你需要使用可邮寄对象的 <code v-pre>to</code> 方法指定消息接收者：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>InvoicePaid</span> <span class="token keyword">as</span> InvoicePaidMailable<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件表现形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Mailable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaidMailable</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="可邮寄对象和按需通知" tabindex="-1"><a class="header-anchor" href="#可邮寄对象和按需通知"><span>可邮寄对象和按需通知</span></a></h4>
<p>如果你正在发送<a href="#on-demand-notifications">按需通知</a>，则提供给<code v-pre>toMail</code>方法的<code v-pre>$notifiable</code>实例将是<code v-pre>Illuminate\Notifications\AnonymousNotifiable</code>的一个实例，它提供了一个<code v-pre>routeNotificationFor</code>方法，该方法可用于检索应将按需通知发送到的电子邮件地址：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>InvoicePaid</span> <span class="token keyword">as</span> InvoicePaidMailable<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>AnonymousNotifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件表现形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Mailable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$address</span> <span class="token operator">=</span> <span class="token variable">$notifiable</span> <span class="token keyword">instanceof</span> <span class="token class-name">AnonymousNotifiable</span></span>
<span class="line">            <span class="token operator">?</span> <span class="token variable">$notifiable</span><span class="token operator">-></span><span class="token function">routeNotificationFor</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token punctuation">:</span> <span class="token variable">$notifiable</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaidMailable</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$address</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="预览邮件通知" tabindex="-1"><a class="header-anchor" href="#预览邮件通知"><span>预览邮件通知</span></a></h3>
<p>设计邮件通知模板时，可以像典型的 Blade 模板一样在浏览器中快速预览呈现的邮件消息。出于这个原因，Laravel 允许你直接从路由闭包或控制器返回由邮件通知生成的任何邮件消息。当返回一个 <code v-pre>MailMessage</code> 时，它将在浏览器中呈现和显示，让你可以快速预览其设计，无需将其发送到实际的电子邮件地址：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Invoice</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>InvoicePaid</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/notification'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$invoice</span> <span class="token operator">=</span> <span class="token class-name static-context">Invoice</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">toMail</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token operator">-></span><span class="token property">user</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="markdown-邮件通知" tabindex="-1"><a class="header-anchor" href="#markdown-邮件通知"><span>Markdown 邮件通知</span></a></h2>
<p>Markdown 邮件通知允许你利用邮件通知的预构建模板，同时为你提供编写更长、定制化消息的自由。由于这些消息是用 Markdown 写的，因此 Laravel 能够为消息呈现漂亮、响应式的 HTML 模板，同时还会自动生成一个纯文本的副本。</p>
<h3 id="生成消息" tabindex="-1"><a class="header-anchor" href="#生成消息"><span>生成消息</span></a></h3>
<p>要生成具有相应 Markdown 模板的通知，可以使用 <code v-pre>make:notification</code> Artisan 命令的 <code v-pre>--markdown</code> 选项：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:notification InvoicePaid <span class="token parameter variable">--markdown</span><span class="token operator">=</span>mail.invoice.paid</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>与所有其他邮件通知一样，使用 Markdown 模板的通知应该在其通知类上定义一个 <code v-pre>toMail</code> 方法。但是，不要使用 <code v-pre>line</code> 和 <code v-pre>action</code> 方法构建通知，而是使用 <code v-pre>markdown</code> 方法指定应该使用的 Markdown 模板的名称。你希望提供给模板的数据数组可以作为该方法的第二个参数传递：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件表现形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/invoice/'</span><span class="token operator">.</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">subject</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'发票支付'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail.invoice.paid'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token variable">$url</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="编写消息" tabindex="-1"><a class="header-anchor" href="#编写消息"><span>编写消息</span></a></h3>
<p>Markdown 邮件通知使用 Blade 组件和 Markdown 语法的组合，可以让你在利用 Laravel 的预构建通知组件的同时，轻松构建通知：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;x-mail::message&gt;</span>
<span class="line"># 发票支付</span>
<span class="line"></span>
<span class="line">你的发票已支付!</span>
<span class="line"></span>
<span class="line">&lt;x-mail::button :url=&quot;$url&quot;&gt;</span>
<span class="line">查看发票</span>
<span class="line">&lt;/x-mail::button&gt;</span>
<span class="line"></span>
<span class="line">谢谢，&lt;br&gt;</span>
<span class="line">{{ config('app.name') }}</span>
<span class="line">&lt;/x-mail::message&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="button-组件" tabindex="-1"><a class="header-anchor" href="#button-组件"><span>Button 组件</span></a></h4>
<p>Button 组件会呈现一个居中的按钮链接。该组件接受两个参数，<code v-pre>url</code> 和一个可选的 <code v-pre>color</code>。支持的颜色有 <code v-pre>primary</code>、<code v-pre>green</code> 和 <code v-pre>red</code>。你可以在通知中添加任意数量的 Button 组件：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;x-mail::button :url=&quot;$url&quot; color=&quot;green&quot;&gt;</span>
<span class="line">查看发票</span>
<span class="line">&lt;/x-mail::button&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="panel-组件" tabindex="-1"><a class="header-anchor" href="#panel-组件"><span>Panel 组件</span></a></h4>
<p>Panel 组件会在通知中呈现给定的文本块，并在面板中以稍微不同的背景颜色呈现。这让你可以引起读者对特定文本块的注意：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;x-mail::panel&gt;</span>
<span class="line">这是面板内容。</span>
<span class="line">&lt;/x-mail::panel&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="table-组件" tabindex="-1"><a class="header-anchor" href="#table-组件"><span>Table 组件</span></a></h4>
<p>Table 组件允许你将 Markdown 表格转换为 HTML 表格。该组件接受 Markdown 表格作为其内容。可以使用默认的 Markdown 表格对齐语法来支持表格列对齐：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;x-mail::table&gt;</span>
<span class="line">| Laravel       | Table         | Example  |</span>
<span class="line">| ------------- |:-------------:| --------:|</span>
<span class="line">| Col 2 is      | Centered      | $10      |</span>
<span class="line">| Col 3 is      | Right-Aligned | $20      |</span>
<span class="line">&lt;/x-mail::table&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="定制组件" tabindex="-1"><a class="header-anchor" href="#定制组件"><span>定制组件</span></a></h3>
<p>你可以将所有的 Markdown 通知组件导出到自己的应用程序进行定制。要导出组件，请使用 <code v-pre>vendor:publish</code> Artisan 命令来发布 <code v-pre>laravel-mail</code> 资源标记：</p>
<p>这个命令会将 Markdown 邮件组件发布到 <code v-pre>resources/views/vendor/mail</code> 目录下。<code v-pre>mail</code> 目录将包含一个 <code v-pre>html</code> 和一个 <code v-pre>text</code> 目录，每个目录都包含其可用组件的各自表示形式。你可以自由地按照自己的喜好定制这些组件。</p>
<h4 id="定制-css-样式" tabindex="-1"><a class="header-anchor" href="#定制-css-样式"><span>定制 CSS 样式</span></a></h4>
<p>在导出组件之后，<code v-pre>resources/views/vendor/mail/html/themes</code> 目录将包含一个 <code v-pre>default.css</code> 文件。你可以在此文件中自定义 CSS 样式，你的样式将自动被内联到 Markdown 通知的 HTML 表示中。</p>
<p>如果你想为 Laravel 的 Markdown 组件构建一个全新的主题，可以在 <code v-pre>html/themes</code> 目录中放置一个 CSS 文件。命名并保存 CSS 文件后，更新 <code v-pre>mail</code> 配置文件的 <code v-pre>theme</code> 选项以匹配你的新主题的名称。</p>
<p>要为单个通知自定义主题，可以在构建通知的邮件消息时调用 <code v-pre>theme</code> 方法。<code v-pre>theme</code> 方法接受应该在发送通知时使用的主题名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的邮件表现形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">theme</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'发票'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">subject</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'发票支付'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail.invoice.paid'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token variable">$url</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="数据库通知" tabindex="-1"><a class="header-anchor" href="#数据库通知"><span>数据库通知</span></a></h2>
<h3 id="前提条件" tabindex="-1"><a class="header-anchor" href="#前提条件"><span>前提条件</span></a></h3>
<p><code v-pre>database</code> 通知渠道将通知信息存储在一个数据库表中。该表将包含通知类型以及描述通知的 JSON 数据结构等信息。</p>
<p>你可以查询该表，在你的应用程序用户界面中显示通知。但是，在此之前，你需要创建一个数据库表来保存你的通知。你可以使用 <code v-pre>notifications:table</code> 命令生成一个适当的表模式的 <a href="https://learnku.com/docs/laravel/10.x/migrations" target="_blank" rel="noopener noreferrer">迁移</a>：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan notifications:table</span>
<span class="line"></span>
<span class="line">php artisan migrate</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="格式化数据库通知" tabindex="-1"><a class="header-anchor" href="#格式化数据库通知"><span>格式化数据库通知</span></a></h3>
<p>如果一个通知支持被存储在一个数据库表中，你应该在通知类上定义一个 <code v-pre>toDatabase</code> 或 <code v-pre>toArray</code> 方法。这个方法将接收一个 <code v-pre>$notifiable</code> 实体，并应该返回一个普通的 PHP 数组。返回的数组将被编码为 JSON，并存储在你的 <code v-pre>notifications</code> 表的 <code v-pre>data</code> 列中。让我们看一个 <code v-pre>toArray</code> 方法的例子：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的数组表示形式。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, mixed></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toArray</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'invoice_id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'amount'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">amount</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="todatabase-vs-toarray" tabindex="-1"><a class="header-anchor" href="#todatabase-vs-toarray"><span><code v-pre>toDatabase</code> Vs. <code v-pre>toArray</code></span></a></h4>
<p><code v-pre>toArray</code> 方法也被 <code v-pre>broadcast</code> 频道用来确定要广播到你的 JavaScript 前端的数据。如果你想为 <code v-pre>database</code> 和 <code v-pre>broadcast</code> 频道定义两个不同的数组表示形式，你应该定义一个 <code v-pre>toDatabase</code> 方法，而不是一个 <code v-pre>toArray</code> 方法。</p>
<h3 id="访问通知" tabindex="-1"><a class="header-anchor" href="#访问通知"><span>访问通知</span></a></h3>
<p>一旦通知被存储在数据库中，你需要一个方便的方式从你的可通知实体中访问它们。<code v-pre>Illuminate\Notifications\Notifiable</code> trait 包含在 Laravel 的默认 <code v-pre>App\Models\User</code> 模型中，它包括一个 <code v-pre>notifications</code> <a href="https://learnku.com/docs/laravel/10.x/eloquent-relationships" target="_blank" rel="noopener noreferrer">Eloquent 关联</a>，返回实体的通知。要获取通知，你可以像访问任何其他 Eloquent 关系一样访问此方法。默认情况下，通知将按照 <code v-pre>created_at</code> 时间戳排序，最新的通知位于集合的开头：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">notifications</span> <span class="token keyword">as</span> <span class="token variable">$notification</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$notification</span><span class="token operator">-></span><span class="token property">type</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你想要只检索「未读」的通知，你可以使用 <code v-pre>unreadNotifications</code> 关系。同样，这些通知将按照 <code v-pre>created_at</code> 时间戳排序，最新的通知位于集合的开头：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">unreadNotifications</span> <span class="token keyword">as</span> <span class="token variable">$notification</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$notification</span><span class="token operator">-></span><span class="token property">type</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
要从你的 JavaScript 客户端访问你的通知，你应该为你的应用程序定义一个通知控制器，该控制器返回一个可通知实体的通知，如当前用户。然后，你可以从你的 JavaScript 客户端向该控制器的 URL 发送 HTTP 请求。</p>
</blockquote>
<h3 id="将通知标记为已读" tabindex="-1"><a class="header-anchor" href="#将通知标记为已读"><span>将通知标记为已读</span></a></h3>
<p>通常，当用户查看通知时，你希望将通知标记为「已读」。<code v-pre>Illuminate\Notifications\Notifiable</code> trait 提供了一个 <code v-pre>markAsRead</code> 方法，该方法将更新通知的数据库记录上的 <code v-pre>read_at</code> 列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">unreadNotifications</span> <span class="token keyword">as</span> <span class="token variable">$notification</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$notification</span><span class="token operator">-></span><span class="token function">markAsRead</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然而，你可以直接在通知集合上使用 <code v-pre>markAsRead</code> 方法，而不是遍历每个通知：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">unreadNotifications</span><span class="token operator">-></span><span class="token function">markAsRead</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你也可以使用批量更新查询将所有通知标记为已读而不必从数据库中检索它们：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">unreadNotifications</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'read_at'</span> <span class="token operator">=></span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以使用 <code v-pre>delete</code> 方法将通知删除并从表中完全移除：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notifications</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="广播通知" tabindex="-1"><a class="header-anchor" href="#广播通知"><span>广播通知</span></a></h2>
<h3 id="前提条件-1" tabindex="-1"><a class="header-anchor" href="#前提条件-1"><span>前提条件</span></a></h3>
<p>在广播通知之前，你应该配置并熟悉 Laravel 的 <a href="https://learnku.com/docs/laravel/10.x/broadcasting" target="_blank" rel="noopener noreferrer">事件广播</a> 服务。事件广播提供了一种从你的 JavaScript 前端响应服务器端 Laravel 事件的方法。</p>
<h3 id="格式化广播通知" tabindex="-1"><a class="header-anchor" href="#格式化广播通知"><span>格式化广播通知</span></a></h3>
<p><code v-pre>broadcast</code> 频道使用 Laravel 的 <a href="https://learnku.com/docs/laravel/10.x/broadcasting" target="_blank" rel="noopener noreferrer">事件广播</a> 服务来广播通知，允许你的 JavaScript 前端实时捕获通知。如果通知支持广播，你可以在通知类上定义一个 <code v-pre>toBroadcast</code> 方法。该方法将接收一个 <code v-pre>$notifiable</code> 实体，并应该返回一个 <code v-pre>BroadcastMessage</code> 实例。如果 <code v-pre>toBroadcast</code> 方法不存在，则将使用 <code v-pre>toArray</code> 方法来收集应该广播的数据。返回的数据将被编码为 JSON 并广播到你的 JavaScript 前端。让我们看一个 <code v-pre>toBroadcast</code> 方法的示例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>BroadcastMessage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的可广播表示形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toBroadcast</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">BroadcastMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">BroadcastMessage</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'invoice_id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'amount'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">amount</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="广播队列配置" tabindex="-1"><a class="header-anchor" href="#广播队列配置"><span>广播队列配置</span></a></h4>
<p>所有广播通知都会被排队等待广播。如果你想配置用于排队广播操作的队列连接或队列名称，你可以使用 <code v-pre>BroadcastMessage</code> 的 <code v-pre>onConnection</code> 和 <code v-pre>onQueue</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">BroadcastMessage</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">onConnection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'sqs'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">onQueue</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'broadcasts'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义通知类型" tabindex="-1"><a class="header-anchor" href="#自定义通知类型"><span>自定义通知类型</span></a></h4>
<p>除了你指定的数据之外，所有广播通知还包含一个 <code v-pre>type</code> 字段，其中包含通知的完整类名。如果你想要自定义通知的 <code v-pre>type</code>，可以在通知类上定义一个 <code v-pre>broadcastType</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取正在广播的通知类型。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">broadcastType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token string single-quoted-string">'broadcast.message'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="监听通知" tabindex="-1"><a class="header-anchor" href="#监听通知"><span>监听通知</span></a></h3>
<p>通知会以 <code v-pre>{notifiable}.{id}</code> 的格式在一个私有频道上广播。因此，如果你向一个 ID 为 <code v-pre>1</code> 的 <code v-pre>App\Models\User</code> 实例发送通知，通知将在 <code v-pre>App.Models.User.1</code> 私有频道上广播。当使用 <a href="https://learnku.com/docs/laravel/10.x/broadcastingmd#client-side-installation" target="_blank" rel="noopener noreferrer">Laravel Echo</a> 时，你可以使用 <code v-pre>notification</code> 方法轻松地在频道上监听通知：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">Echo</span><span class="token operator">.</span><span class="token keyword">private</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'App.Models.User.'</span> <span class="token operator">+</span> userId<span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">notification</span><span class="token punctuation">(</span><span class="token punctuation">(</span>notification<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span></span>
<span class="line">        console<span class="token operator">.</span><span class="token function">log</span><span class="token punctuation">(</span>notification<span class="token operator">.</span>type<span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义通知频道" tabindex="-1"><a class="header-anchor" href="#自定义通知频道"><span>自定义通知频道</span></a></h4>
<p>如果你想自定义实体的广播通知在哪个频道上广播，可以在可通知实体上定义一个 <code v-pre>receivesBroadcastNotificationsOn</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Broadcasting<span class="token punctuation">\</span>PrivateChannel</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 用户接收通知广播的频道。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">receivesBroadcastNotificationsOn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token string single-quoted-string">'users.'</span><span class="token operator">.</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="短信通知" tabindex="-1"><a class="header-anchor" href="#短信通知"><span>短信通知</span></a></h2>
<h3 id="先决条件" tabindex="-1"><a class="header-anchor" href="#先决条件"><span>先决条件</span></a></h3>
<p>Laravel 中发送短信通知是由 <a href="https://www.vonage.com/" target="_blank" rel="noopener noreferrer">Vonage</a>（之前称为 Nexmo）驱动的。在通过 Vonage 发送通知之前，你需要安装 <code v-pre>laravel/vonage-notification-channel</code> 和 <code v-pre>guzzlehttp/guzzle</code> 包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> laravel<span class="token operator">/</span>vonage<span class="token operator">-</span>notification<span class="token operator">-</span>channel guzzlehttp<span class="token operator">/</span>guzzle</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>该包包括一个 <a href="https://github.com/laravel/vonage-notification-channel/blob/3.x/config/vonage.php" target="_blank" rel="noopener noreferrer">配置文件</a>。但是，你不需要将此配置文件导出到自己的应用程序。你可以简单地使用 <code v-pre>VONAGE_KEY</code> 和 <code v-pre>VONAGE_SECRET</code> 环境变量来定义 Vonage 的公共和私有密钥。</p>
<p>定义好密钥后，你可以设置一个 <code v-pre>VONAGE_SMS_FROM</code> 环境变量，该变量定义了你发送 SMS 消息的默认电话号码。你可以在 Vonage 控制面板中生成此电话号码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token constant">VONAGE_SMS_FROM</span><span class="token operator">=</span><span class="token number">15556666666</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="格式化短信通知" tabindex="-1"><a class="header-anchor" href="#格式化短信通知"><span>格式化短信通知</span></a></h3>
<p>如果通知支持作为 SMS 发送，你应该在通知类上定义一个 <code v-pre>toVonage</code> 方法。此方法将接收一个 <code v-pre>$notifiable</code> 实体并应返回一个 <code v-pre>Illuminate\Notifications\Messages\VonageMessage</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>VonageMessage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Vonage / SMS 表达式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toVonage</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">VonageMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">VonageMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'你的短信内容'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="unicode-内容" tabindex="-1"><a class="header-anchor" href="#unicode-内容"><span>Unicode 内容</span></a></h4>
<p>如果你的 SMS 消息将包含 unicode 字符，你应该在构造 <code v-pre>VonageMessage</code> 实例时调用 <code v-pre>unicode</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>VonageMessage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Vonage / SMS 表达式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toVonage</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">VonageMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">VonageMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'你的统一码消息'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">unicode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义「来源」号码" tabindex="-1"><a class="header-anchor" href="#自定义「来源」号码"><span>自定义「来源」号码</span></a></h3>
<p>如果你想从一个不同于 <code v-pre>VONAGE_SMS_FROM</code> 环境变量所指定的电话号码发送通知，你可以在 <code v-pre>VonageMessage</code> 实例上调用 <code v-pre>from</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>VonageMessage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Vonage / SMS 表达式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toVonage</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">VonageMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">VonageMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'您的短信内容'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'15554443333'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="添加客户关联" tabindex="-1"><a class="header-anchor" href="#添加客户关联"><span>添加客户关联</span></a></h3>
<p>如果你想跟踪每个用户、团队或客户的消费，你可以在通知中添加「客户关联」。Vonage 将允许你使用这个客户关联生成报告，以便你能更好地了解特定客户的短信使用情况。客户关联可以是任何字符串，最多 40 个字符。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>VonageMessage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Vonage / SMS 表达式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toVonage</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">VonageMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">VonageMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">clientReference</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword type-casting">string</span><span class="token punctuation">)</span> <span class="token variable">$notifiable</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'你的短信内容'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="路由短信通知" tabindex="-1"><a class="header-anchor" href="#路由短信通知"><span>路由短信通知</span></a></h3>
<p>要将 Vonage 通知路由到正确的电话号码，请在你的通知实体上定义 <code v-pre>routeNotificationForVonage</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * Vonage 通道的路由通知。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">routeNotificationForVonage</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Notification</span> <span class="token variable">$notification</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">phone_number</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="slack-通知" tabindex="-1"><a class="header-anchor" href="#slack-通知"><span>Slack 通知</span></a></h2>
<h3 id="先决条件-1" tabindex="-1"><a class="header-anchor" href="#先决条件-1"><span>先决条件</span></a></h3>
<p>在你可以通过 Slack 发送通知之前，你必须通过 Composer 安装 Slack 通知通道：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require laravel/slack-notification-channel</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你还需要为你的团队创建一个 <a href="https://api.slack.com/apps?new_app=1" target="_blank" rel="noopener noreferrer">Slack 应用</a>。创建应用后，你应该为工作区配置一个「传入 Webhook」。 然后，Slack 将为你提供一个 webhook URL，你可以在 <a href="#routing-slack-notifications">路由 Slack 通知</a> 时使用该 URL。</p>
<h3 id="格式化-slack-通知" tabindex="-1"><a class="header-anchor" href="#格式化-slack-通知"><span>格式化 Slack 通知</span></a></h3>
<p>如果通知支持作为 Slack 消息发送，你应在通知类上定义 <code v-pre>toSlack</code> 方法。此方法将接收一个 <code v-pre>$notifiable</code> 实体并应返回一个 <code v-pre>Illuminate\Notifications\Messages\SlackMessage</code> 实例。Slack 消息可能包含文本内容以及格式化附加文本或字段数组的「附件」。 让我们看一个基本的 <code v-pre>toSlack</code> 示例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>SlackMessage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Slack 表达式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toSlack</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">SlackMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'你的一张发票已经付款了！'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="slack-附件" tabindex="-1"><a class="header-anchor" href="#slack-附件"><span>Slack 附件</span></a></h3>
<p>你还可以向 Slack 消息添加「附件」。附件提供比简单文本消息更丰富的格式选项。在这个例子中，我们将发送一个关于应用程序中发生的异常的错误通知，包括一个链接，以查看有关异常的更多详细信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>SlackAttachment</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>SlackMessage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Slack 表示形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toSlack</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">SlackMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/exceptions/'</span><span class="token operator">.</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">exception</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">error</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'哎呀！出了问题。'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">attachment</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">SlackAttachment</span> <span class="token variable">$attachment</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token variable">$attachment</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'例外：文件未找到'</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">)</span></span>
<span class="line">                               <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'文件 [background.jpg] 未找到。'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>附件还允许你指定应呈现给用户的数据数组。 给定的数据将以表格形式呈现，以便于阅读：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>SlackAttachment</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>SlackMessage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Slack 表示形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toSlack</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">SlackMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/invoices/'</span><span class="token operator">.</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">success</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'你的一张发票已经付款了！'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">attachment</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">SlackAttachment</span> <span class="token variable">$attachment</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token variable">$attachment</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Invoice 1322'</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">)</span></span>
<span class="line">                               <span class="token operator">-></span><span class="token function">fields</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">                                    <span class="token string single-quoted-string">'Title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Server Expenses'</span><span class="token punctuation">,</span></span>
<span class="line">                                    <span class="token string single-quoted-string">'Amount'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'$1,234'</span><span class="token punctuation">,</span></span>
<span class="line">                                    <span class="token string single-quoted-string">'Via'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'American Express'</span><span class="token punctuation">,</span></span>
<span class="line">                                    <span class="token string single-quoted-string">'Was Overdue'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">':-1:'</span><span class="token punctuation">,</span></span>
<span class="line">                                <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="markdown-附件内容" tabindex="-1"><a class="header-anchor" href="#markdown-附件内容"><span>Markdown 附件内容</span></a></h4>
<p>如果你的附件字段中包含 Markdown，则可以使用 <code v-pre>markdown</code> 方法指示 Slack 解析和显示给定的附件字段为 Markdown 格式的文本。此方法接受的值是：<code v-pre>pretext</code>、<code v-pre>text</code>和/或<code v-pre>fields</code>。有关 Slack 附件格式的更多信息，请查看 <a href="https://api.slack.com/docs/message-formatting#message_formatting" target="_blank" rel="noopener noreferrer">Slack API 文档</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>SlackAttachment</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>SlackMessage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Slack 表示形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toSlack</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">SlackMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/exceptions/'</span><span class="token operator">.</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">exception</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">error</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Whoops! Something went wrong.'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">attachment</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">SlackAttachment</span> <span class="token variable">$attachment</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$url</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token variable">$attachment</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Exception: File Not Found'</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">)</span></span>
<span class="line">                               <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'File [background.jpg] was *not found*.'</span><span class="token punctuation">)</span></span>
<span class="line">                               <span class="token operator">-></span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'text'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="路由-slack-通知" tabindex="-1"><a class="header-anchor" href="#路由-slack-通知"><span>路由 Slack 通知</span></a></h3>
<p>为了将 Slack 通知路由到正确的 Slack 团队和频道，请在你的通知实体上定义一个 <code v-pre>routeNotificationForSlack</code> 方法。它应该返回要传送通知的 Webhook URL。Webhook URL 可以通过向你的 Slack 团队添加「传入 Webhook」服务来生成：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 路由 Slack 频道的通知。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">routeNotificationForSlack</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Notification</span> <span class="token variable">$notification</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token string single-quoted-string">'https://hooks.slack.com/services/...'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="本地化通知" tabindex="-1"><a class="header-anchor" href="#本地化通知"><span>本地化通知</span></a></h2>
<p>Laravel 允许你在除了当前请求语言环境之外的其他语言环境中发送通知，甚至在通知被队列化的情况下也能记住此语言环境。</p>
<p>为了实现这一功能，<code v-pre>Illuminate\Notifications\Notification</code> 类提供了 <code v-pre>locale</code> 方法来设置所需的语言环境。在通知被评估时，应用程序将切换到此语言环境，然后在评估完成后恢复到以前的语言环境：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">locale</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'es'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>通过 <code v-pre>Notification</code> 门面，也可以实现多个通知实体的本地化：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">locale</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'es'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token variable">$users</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="用户首选语言环境" tabindex="-1"><a class="header-anchor" href="#用户首选语言环境"><span>用户首选语言环境</span></a></h3>
<p>有时，应用程序会存储每个用户的首选区域设置。通过在你的可通知模型上实现 <code v-pre>HasLocalePreference</code> 合同，你可以指示 Laravel 在发送通知时使用此存储的区域设置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Translation<span class="token punctuation">\</span>HasLocalePreference</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span> <span class="token keyword">implements</span> <span class="token class-name">HasLocalePreference</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取用户的首选语言环境。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">preferredLocale</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">locale</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>翻译：一旦你实现了这个接口，当发送通知和邮件到该模型时，Laravel 会自动使用首选语言环境。因此，在使用此接口时不需要调用<code v-pre>locale</code>方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="测试" tabindex="-1"><a class="header-anchor" href="#测试"><span>测试</span></a></h2>
<p>你可以使用 <code v-pre>Notification</code> 门面的 <code v-pre>fake</code> 方法来阻止通知被发送。通常情况下，发送通知与你实际测试的代码无关。很可能，只需要断言 Laravel 被指示发送了给定的通知即可。</p>
<p>在调用 <code v-pre>Notification</code> 门面的 <code v-pre>fake</code> 方法后，你可以断言已经被告知将通知发送给用户，甚至检查通知接收到的数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">Tests<span class="token punctuation">\</span>Feature</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>OrderShipped</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Tests<span class="token punctuation">\</span>TestCase</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ExampleTest</span> <span class="token keyword">extends</span> <span class="token class-name">TestCase</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">test_orders_can_be_shipped</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 执行订单发货...</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 断言没有发送通知...</span></span>
<span class="line">        <span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">assertNothingSent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 断言通知已发送给给定用户...</span></span>
<span class="line">        <span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">assertSentTo</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token punctuation">[</span><span class="token variable">$user</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token class-name static-context">OrderShipped</span><span class="token operator">::</span><span class="token keyword">class</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 断言未发送通知...</span></span>
<span class="line">        <span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">assertNotSentTo</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token punctuation">[</span><span class="token variable">$user</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token class-name static-context">AnotherNotification</span><span class="token operator">::</span><span class="token keyword">class</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 断言已发送给定数量的通知...</span></span>
<span class="line">        <span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">assertCount</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以通过向 <code v-pre>assertSentTo</code> 或 <code v-pre>assertNotSentTo</code> 方法传递一个闭包来断言发送了符合给定「真实性测试」的通知。如果发送了至少一个符合给定真实性测试的通知，则断言将成功：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">assertSentTo</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token variable">$user</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">OrderShipped</span> <span class="token variable">$notification</span><span class="token punctuation">,</span> <span class="token keyword type-hint">array</span> <span class="token variable">$channels</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$notification</span><span class="token operator">-></span><span class="token property">order</span><span class="token operator">-></span><span class="token property">id</span> <span class="token operator">===</span> <span class="token variable">$order</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="按需通知-1" tabindex="-1"><a class="header-anchor" href="#按需通知-1"><span>按需通知</span></a></h4>
<p>如果你正在测试的代码发送 <a href="https://chat.openai.com/chat#on-demand-notifications" target="_blank" rel="noopener noreferrer">即时通知</a>，你可以使用 <code v-pre>assertSentOnDemand</code> 方法测试是否发送了即时通知：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">assertSentOnDemand</span><span class="token punctuation">(</span><span class="token class-name static-context">OrderShipped</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>通过将闭包作为 <code v-pre>assertSentOnDemand</code> 方法的第二个参数传递，你可以确定是否将即时通知发送到了正确的 「route」 地址：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">assertSentOnDemand</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token class-name static-context">OrderShipped</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">OrderShipped</span> <span class="token variable">$notification</span><span class="token punctuation">,</span> <span class="token keyword type-hint">array</span> <span class="token variable">$channels</span><span class="token punctuation">,</span> <span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$notifiable</span><span class="token operator">-></span><span class="token property">routes</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'mail'</span><span class="token punctuation">]</span> <span class="token operator">===</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="通知事件" tabindex="-1"><a class="header-anchor" href="#通知事件"><span>通知事件</span></a></h2>
<h4 id="通知发送事件" tabindex="-1"><a class="header-anchor" href="#通知发送事件"><span>通知发送事件</span></a></h4>
<p>发送通知时，通知系统会调度 <code v-pre>Illuminate\Notifications\Events\NotificationSending</code> <a href="https://learnku.com/docs/laravel/10.x/events" target="_blank" rel="noopener noreferrer">事件</a>。 这包含「可通知」实体和通知实例本身。 你可以在应用程序的 <code v-pre>EventServiceProvider</code> 中为该事件注册监听器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Listeners<span class="token punctuation">\</span>CheckNotificationStatus</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Events<span class="token punctuation">\</span>NotificationSending</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 应用程序的事件侦听器映射。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$listen</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token class-name static-context">NotificationSending</span><span class="token operator">::</span><span class="token keyword">class</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token class-name static-context">CheckNotificationStatus</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果 <code v-pre>NotificationSending</code> 事件的监听器从它的 <code v-pre>handle</code> 方法返回 <code v-pre>false</code>，通知将不会被发送：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Events<span class="token punctuation">\</span>NotificationSending</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 处理事件。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token class-name type-declaration">NotificationSending</span> <span class="token variable">$event</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token constant boolean">false</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在事件监听器中，你可以访问事件的 <code v-pre>notifiable</code>、<code v-pre>notification</code> 和 <code v-pre>channel</code> 属性，以了解有关通知接收者或通知本身的更多信息。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 处理事件。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token class-name type-declaration">NotificationSending</span> <span class="token variable">$event</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// $event->channel</span></span>
<span class="line">    <span class="token comment">// $event->notifiable</span></span>
<span class="line">    <span class="token comment">// $event->notification</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="通知发送事件-1" tabindex="-1"><a class="header-anchor" href="#通知发送事件-1"><span>通知发送事件</span></a></h4>
<p>当通知被发送时，通知系统会触发 <code v-pre>Illuminate\Notifications\Events\NotificationSent</code> <a href="https://learnku.com/docs/laravel/10.x/events" target="_blank" rel="noopener noreferrer">事件</a>，其中包含 「notifiable」 实体和通知实例本身。你可以在 <code v-pre>EventServiceProvider</code> 中注册此事件的监听器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Listeners<span class="token punctuation">\</span>LogNotification</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Events<span class="token punctuation">\</span>NotificationSent</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 应用程序的事件侦听器映射。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">array</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$listen</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token class-name static-context">NotificationSent</span><span class="token operator">::</span><span class="token keyword">class</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token class-name static-context">LogNotification</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
在 <code v-pre>EventServiceProvider</code> 中注册了监听器之后，可以使用 event:generate Artisan 命令快速生成监听器类。</p>
</blockquote>
<p>在事件监听器中，你可以访问事件上的 <code v-pre>notifiable</code>、<code v-pre>notification</code>、<code v-pre>channel</code> 和 <code v-pre>response</code> 属性，以了解更多有关通知收件人或通知本身的信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 处理事件。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token class-name type-declaration">NotificationSent</span> <span class="token variable">$event</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// $event->channel</span></span>
<span class="line">    <span class="token comment">// $event->notifiable</span></span>
<span class="line">    <span class="token comment">// $event->notification</span></span>
<span class="line">    <span class="token comment">// $event->response</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="自定义频道" tabindex="-1"><a class="header-anchor" href="#自定义频道"><span>自定义频道</span></a></h2>
<p>Laravel 提供了一些通知频道，但你可能想编写自己的驱动程序，以通过其他频道传递通知。Laravel 让这变得简单。要开始，定义一个包含 <code v-pre>send</code> 方法的类。该方法应接收两个参数：<code v-pre>$notifiable</code> 和 <code v-pre>$notification</code>。</p>
<p>在 <code v-pre>send</code> 方法中，你可以调用通知上的方法来检索一个由你的频道理解的消息对象，然后按照你希望的方式将通知发送给 <code v-pre>$notifiable</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Notifications</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">VoiceChannel</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 发送给定的通知</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">send</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Notification</span> <span class="token variable">$notification</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$message</span> <span class="token operator">=</span> <span class="token variable">$notification</span><span class="token operator">-></span><span class="token function">toVoice</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 将通知发送给 $notifiable 实例...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦你定义了你的通知频道类，你可以从你的任何通知的 <code v-pre>via</code> 方法返回该类的名称。在这个例子中，你的通知的 <code v-pre>toVoice</code> 方法可以返回你选择来表示语音消息的任何对象。例如，你可以定义自己的 <code v-pre>VoiceMessage</code> 类来表示这些消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Notifications</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>VoiceMessage</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>VoiceChannel</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>ShouldQueue</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">InvoicePaid</span> <span class="token keyword">extends</span> <span class="token class-name">Notification</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取通知频道</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">via</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token class-name static-context">VoiceChannel</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取通知的语音表示形式</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toVoice</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">VoiceMessage</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/10.x/notifications/14870" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/no...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/10.x/notifications/14870" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/no...</a></p>
</blockquote>
</div></template>


